
public class InterruptibleThread extends Thread { 
	private boolean interruptsEnabled = false; 
	private boolean interruptPending = false; 
	
	public static void enableInterrupts() { 
		InterruptibleThread self = (InterruptibleThread) InterruptibleThread.currentThread(); 
		synchronized (self) { 
			self.interruptsEnabled = true; 
			if (self.interruptPending) 
				self.interrupt(); 
			self.interruptPending = false; 
		} 
	} 
	public static void disableInterrupts() { 
		InterruptibleThread self = (InterruptibleThread) InterruptibleThread.currentThread(); 
		synchronized (self) { 
			if (interrupted()) 
				self.interruptPending = true; 
			self.interruptsEnabled = false; 
		} 
	} 
	public synchronized void interrupt() { 
		if (interruptsEnabled) 
			super.interrupt(); 
		else 
			interruptPending = true; 
	}
}